package com.ayoka.adapter;

import android.widget.TextView;

import com.ayoka.R;
import com.ayoka.bean.CourseAssignmentBean;
import com.ayoka.bean.CourseAssignmentBean.OptionBean;
import com.jcp.baselibrary.brvah.BaseQuickAdapter;
import com.jcp.baselibrary.brvah.BaseViewHolder;

import java.util.List;

/**
 * Created by jinchunping on 2018/4/11.
 */

public class CourseAssignmentChoiceOptionAdapter extends BaseQuickAdapter<OptionBean, BaseViewHolder> {

    private int choiceMode;

    public CourseAssignmentChoiceOptionAdapter(int choiceMode) {
        super(R.layout.item_course_assignment_choice_option, null);
        this.choiceMode = choiceMode;
    }

    @Override
    protected void convert(final BaseViewHolder holder, final OptionBean item) {
        holder.setText(R.id.tv_choice_option, item.Option)
                .setSelected(R.id.tv_choice_option, item.isSelected);
        TextView tvOption = holder.getView(R.id.tv_choice_option);
        if (choiceMode == CourseAssignmentBean.MULTI_CHOICE) {
            tvOption.setCompoundDrawablesWithIntrinsicBounds(R.drawable.selector_check_box, 0, 0, 0);
        } else if (choiceMode == CourseAssignmentBean.SINGLE_CHOICE) {
            tvOption.setCompoundDrawablesWithIntrinsicBounds(R.drawable.selector_radio_button, 0, 0, 0);
        }
    }

    private int previousSelectedIndex;

    public void setSingleChoice(int position) {
        getData().get(position).isSelected = true;
        notifyItemChanged(position);
        if (position != previousSelectedIndex) {
            getData().get(previousSelectedIndex).isSelected = false;
            notifyItemChanged(previousSelectedIndex);
        }
        previousSelectedIndex = position;
    }

    public void setMultiChoice(int position) {
        boolean isSelected = getItem(position).isSelected;
        getItem(position).isSelected = !isSelected;
        notifyDataSetChanged();
    }
}
